package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService=new BrandServiceImpl();

    /**
     * 查询所有
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1调用service查询
        List<Brand> brands = brandService.selectAll();
        //2将对象转为json数据
        String jsonString = JSON.toJSONString(brands);
        //3 将数据回显到浏览器 另外要注意中文编码
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 新增功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1接收表单数据 是json格式
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        //2 将s转换为brand对象
        Brand brand = JSON.parseObject(s, Brand.class);

        //3 调用service方法
        brandService.add(brand);
        //4向页面响应成功的标识
        response.getWriter().write("success");

    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1接收表单数据 是json格式
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        //2 将s转换数组
        int[] ids = JSON.parseObject(s, int[].class);
        //3 调用service方法
        brandService.deleteByIds(ids);
        //4向页面响应成功的标识
        response.getWriter().write("success");
    }


    /**
     * 分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1调用service查询
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //调用方法查询
        PageBean<Brand> brandPageBean = brandService.selectByPage(currentPage, pageSize);
        //2将对象转为json数据
        String jsonString = JSON.toJSONString(brandPageBean);
        //3 将数据回显到浏览器 另外要注意中文编码
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1调用service查询
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //1接收表单数据 是json格式
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        //2 将s转换数组
        Brand brand = JSON.parseObject(s, Brand.class);

        //调用方法查询
        PageBean<Brand> brandPageBean = brandService.selectByPageAndCondition(currentPage, pageSize,brand);
        //2将对象转为json数据
        String jsonString = JSON.toJSONString(brandPageBean);
        //3 将数据回显到浏览器 另外要注意中文编码
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }




}
